package org.sfx.rich

import javafx.scene.input.MouseEvent
import javafx.event.EventHandler
import javafx.scene.Node
import javafx.scene.Scene
import org.sfx.geometry.Bounds2D
import org.sfx.rich.Enrich._

class RichNode(underlying: Node) {
  def onMouseClicked(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def setOnMouseDragged(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def setOnMouseEntered(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def setOnMouseExited(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def setOnMouseMoved(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def setOnMousePressed(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def setOnMouseReleased(f: MouseEvent => Unit) = underlying.setOnMouseClicked(new EventHandler[MouseEvent] {
    def handle(event: MouseEvent): Unit = f(event)
  })
  
  def scene: Option[Scene] = Option(underlying.getScene())
  
  def layoutBounds: Bounds2D = Bounds2D(underlying.getLayoutBounds())
  
  def boundsFromScene: Bounds2D = {
    val layoutBounds = underlying.getLayoutBounds()
    val sceneBounds = underlying.localToScene(layoutBounds)
    
    Bounds2D(sceneBounds)
  }
  
  def boundsFromScreen: Option[Bounds2D] = for {
    scene <- underlying.scene
    sceneMinPoint <- scene.minPointFromScreen
  } yield this.boundsFromScene + sceneMinPoint

  def boundsFromWindow: Option[Bounds2D] = this.scene.map(this.boundsFromScene + _.minPoint)
}
